enyo.kind({
   name: "PsnTrophies.Games",
   kind: "enyo.Control",
   classes: "onyx",
   events: { 
      onSelect: ""
   },
   published: {
       selectedPsnId: ""
   },
   components: [
		{name: "gameScroller", kind: "enyo.Scroller", horizontal: "auto", vertical: "auto", components: [
		   {kind: "onyx.Groupbox", components: [
            {kind: "onyx.GroupboxHeader", name: "groupBoxHeader"},
			   {name: "list", kind: "Repeater", onSetupItem: "getListItem", components: [
				   {kind: "onyx.Item", layoutKind: "FittableRowsLayout", style: "border-bottom-style: inset; border-width: 1px; border-color: grey", onclick: "listItemClick", components: [
				      {kind: "Control", layoutKind: "FittableColumnsLayout", components: [
			   	      {name: "gameImage", kind: "Image", style: "width: 79px; height: 50px;"},
					      {kind: "Control", layoutKind: "FittableRowsLayout", fit: true, components: [
					         {name: "gameTitle", style: "padding-bottom: 1px; padding-left: 1px"},
					         {kind: "enyo.Control", layoutKind: "FittableColumnsLayout", fit: true, style: "padding-bottom: 1px; padding-left: 1px", components: [
							      {name: "progress", kind: "onyx.ProgressBar", barClasses: "onyx-dark", fit: true},
							   ]},
							   {kind: "Control", layoutKind: "FittableColumnsLayout", style: "padding-bottom: 1px; padding-left: 1px", components: [
								   {name: "bronzeImage", kind: "Image", src: "assets/game-bronze.png", style: "width: 28px; height: 30px;"},
								   {name: "bronzeCount"},
								   {name: "silverImage", kind: "Image", src: "assets/game-silver.png", style: "width: 28px; height: 30px;"},
								   {name: "silverCount"},
								   {name: "goldImage", kind: "Image", src: "assets/game-gold.png", style: "width: 28px; height: 30px;"},
								   {name: "goldCount"},
								   {name: "platinumImage", kind: "Image", src: "assets/game-plat.png", style: "width: 28px; height: 30px;"},
								   {name: "platinumCount"}
							   ]}
						   ]}
				      ]}
				   ]}
			   ]}
		   ]}
	   ]}
	],
	create: function() {
	   this.inherited(arguments);
	   
	   // Data for this view
	   this.results = [];
	   this.getGamesRetries = {};
	},
	getGamesService: function(psnId) {
	    this.selectedPsnId = psnId;
	    var url = "http://query.yahooapis.com/v1/public/yql?q=select" 
	                + "%20*%20from%20xml%20where%20url%3D%22http://www.psnapi.com.ar/ps3/api/psn.asmx/getGames?sPSNID=" 
	                + psnId + "%22&format=json&callback=";
	    this.log("URL is: " + url);
	    this.getGamesWebService(url);
	},
	getGamesWebService: function(getURL) {
         this.log("Starting enyo ajax call");
         var request = new enyo.Ajax({
             url: getURL,
             method: "GET",
             handleAs: "json",
         });
         request.response(enyo.bind(this, "getGamesWebService_Success"));
         request.fail(enyo.bind(this, "getGamesWebService_Failure"))
         request.go({});
         this.log("Completed enyo ajax call");
	},
    getGamesWebService_Success: function(inSender, inResponse) {
         this.log("inResponse = " + enyo.json.stringify(inResponse));
         
         if (!inResponse.query || !inResponse.query.results || !inResponse.query.results.ArrayOfGame) {
             this.log("Empty response received from yahoo for getGames.   Retrying with url: " + inSender.url);
             if (!this.getGamesRetries[inSender.url])
                  this.getGamesRetries[inSender.url] = 1;
             else
                  this.getGamesRetries[inSender.url] = this.getGamesRetries[inSender.url] + 1;
             if (this.getGamesRetries[inSender.url] <= 3) {
                  this.log("Retry count is " + this.getGamesRetries[inSender.url]);
                  this.$.getGamesWebService.setUrl(inSender.url);
                  this.$.getGamesWebService.call();
             } else {
                  this.log("Retry count is over 3, giving up.");
             }
             return;
         }
         
         this.results = inResponse.query.results.ArrayOfGame.Game;
         this.log("Got " + this.results.length + " games");
         
         // render list
         this.$.groupBoxHeader.setContent(this.selectedPsnId + "'s Trophies");
         this.$.list.setCount(this.results.length);
    },
    getGamesWebService_Failure: function(inSender, inResponse) {
       this.log(inResponse.error.description);
    },
    getListItem: function(inSender, inEvent) {
       var inIndex = inEvent.index;
       this.log("inIndex is " + inIndex);
      
       if (inIndex < this.results.length) {
          var r = this.results[inIndex];
          if (r) {
             var rowControl = inEvent.item;
             rowControl.$.gameImage.setSrc(r.Image);
             rowControl.$.gameTitle.setContent(r.Title);
             rowControl.$.progress.setProgress(r.Progress);
             rowControl.$.bronzeCount.setContent(r.TrophiesCount.Bronze);
             rowControl.$.silverCount.setContent(r.TrophiesCount.Silver);
             rowControl.$.goldCount.setContent(r.TrophiesCount.Gold);
             rowControl.$.platinumCount.setContent(r.TrophiesCount.Platinum);
            
             return true;
          }
      }
	},
	listItemClick: function(inSender, inEvent) {
      this.log("List item clicked");
      var selectedGame = this.results[inEvent.rowIndex];
      this.log("Selected game = " + selectedGame);
      this.doSelect(selectedGame);
	}
});